"""
顶部信息栏组件
"""
from PyQt6.QtWidgets import QWidget, QHBoxLayout, QLabel
from PyQt6.QtCore import QDateTime, QTimer
from PyQt6.QtGui import QFont


class TopInfoWidget(QWidget):
    """顶部信息栏组件"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setup_ui()
        self.start_timer()
    
    def setup_ui(self):
        """初始化UI"""
        self.setStyleSheet("""
            TopInfoWidget {
                background-color: #ffffff;
                border-bottom: 1px solid #e8e8e8;
                min-height: 80px;
                max-height: 80px;
            }
            QLabel {
                color: #595959;
                font-size: 12px;
                font-family: "Microsoft YaHei";
            }
        """)
        
        layout = QHBoxLayout(self)
        layout.setContentsMargins(20, 0, 20, 0)
        layout.setSpacing(10)
        
        # 创建标题标签
        title_label = QLabel("悠悠捡漏")
        title_label.setStyleSheet("""
            QLabel {
                color: #262626;
                font-size: 14px;
                font-weight: bold;
            }
        """)
        
        # 创建版本标签
        version_label = QLabel("[主界面]")
        version_label.setStyleSheet("""
            QLabel {
                color: #8c8c8c;
                font-size: 12px;
            }
        """)
        
        # 创建时间标签
        self.time_label = QLabel()
        self.time_label.setStyleSheet("""
            QLabel {
                color: #8c8c8c;
                font-size: 12px;
            }
        """)
        
        # 创建用户ID标签
        user_label = QLabel("用户ID:11")
        user_label.setStyleSheet("""
            QLabel {
                color: #8c8c8c;
                font-size: 12px;
            }
        """)
        
        # 添加到布局
        layout.addWidget(title_label)
        layout.addWidget(version_label)
        layout.addStretch()
        layout.addWidget(self.time_label)
        layout.addWidget(user_label)
    
    def start_timer(self):
        """启动定时器更新时间"""
        # 更新当前时间
        self.update_time()
        
        # 创建定时器，每秒更新一次
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_time)
        self.timer.start(1000)  # 1000毫秒 = 1秒
    
    def update_time(self):
        """更新时间显示"""
        current_time = QDateTime.currentDateTime()
        formatted_time = current_time.toString("yyyy/MM/dd hh:mm:ss")
        self.time_label.setText(formatted_time)
